/*
 * Copyright 2009 Al Phillips.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xsd4gorm.model

/**
 *
 * @author Al Phillips
 */
public class DomainClass extends Domain{

    //protected static final String TAB = "    "

    private ArrayList types = new ArrayList()
    String name
    protected Map hasManyMap = new HashMap()
    //def constraintNames = []
    //String source

    public DomainClass(){
    }

    public DomainClass(String name){
        this.name = name
    }
    
    public void addType(DomainType domainType){
        types.add(domainType)
    }

    public ArrayList getTypes(){
        types    
    }

    public Map getHasManyMap(){
        hasManyMap
    }

    /*public String toString (){

        sourceCode "class ${getName()} {\n\n"

        types.each {
            it.generate(this)
        }

        // TODO: this lacks symmetry
        generateHasMany()
        generateConstraints()
        sourceCode "\n}"
        getSourceCode()
    }*/

    /*public DomainType getType(String name){
        return (DomainType)types.get(name)
    }*/

    /*protected void sourceCodeln(source){
        this.source += source + "\n" 
    }

    protected void sourceCode(source){
        this.source += source
    }

    protected String getSourceCode(){
        this.source
    }*/

//    private void generateHasMany(){
//        if(hasManyMap.size() > 0){
//            // TODO: May need to consider this approach if there are two many items in hasMany
//            /*sourceCode += TAB +     "static hasMany = ["
//            int count = 1
//            hasManyMap.each { entry ->
//                sourceCode += "${entry.key}:${entry.value}"
//                if (count < hasManyMap.size()){
//                    sourceCode += ",\n${TAB}                  "
//                }
//                count ++
//            }
//            sourceCode += "]\n"*/
//            source += TAB +     "static hasMany = ${hasManyMap.toMapString()}"
//            source += TAB + "\n"
//        }
//
//    }

//    private void generateConstraints(){
//        sourceCodeln TAB + "static constraints = {"
//        constraints.each {constraint ->
//            sourceCodeln TAB + TAB + constraint
//        }
//        sourceCodeln TAB + "}"
//    }

    private String getName(){
        makeNameClassFriendly(name)
    }

    /*private String classicalName(){
        makeNameClassFriendly(name)
    }*/

    public static String makeNameClassFriendly(String badName){
        def classicalName = badName
        if (classicalName.length() > 1) {
            classicalName[0].toUpperCase() + classicalName[1..classicalName.length()-1]
        }

        else {
            classicalName.toUpperCase()
        }
    }

}